import { useEffect, useRef } from 'react'
import { AiEditor } from 'aieditor'
import 'aieditor/dist/style.css'
import { Button } from 'antd'

const Test = () => {
  //定义 ref
  const divRef = useRef(null)

  //初始化 AiEditor
  useEffect(() => {
    if (divRef.current) {
      const aiEditor = new AiEditor({
        // 基础
        element: divRef.current,
        placeholder: '点击输入内容...',
        content: '',
        contentIsMarkdown: true,
        contentRetention: true,
        contentRetentionKey: 'ai-editor-content',
        draggable: true,
        pasteAsText: false,
        //editable: false,
        //textCounter: () => 0,
        ai: {
          models: {
            spark: {
              appId: '2cab69dd',
              apiKey: 'a3a46d991084612fef1ee16b0ff039bd',
              apiSecret: 'ZDQ4ODYwNzc2MzE2MzA0MTZhM2M3NWI3',
              protocol: 'ws', //或者 wss
              version: 'v4.0' //或者其他
            },
            //gitee: {
            //  endpoint:'https://ai.gitee.com/v1/chat/completions',
            //  //apiKey: '9SFSZKN1HQEQURZGOCSR3SFYJ2HHWDI1ZU2IKZ17'
            //  apiKey: '2TFQTCQZSUMUOOOVBURJZEBVO6VQG9UIXBPQIF3H'
            //}

            custom: {
              url: 'https://ai.gitee.com/v1/chat/completions',
              headers: () => {
                return {
                  'Authorization': 'Bearer 2TFQTCQZSUMUOOOVBURJZEBVO6VQG9UIXBPQIF3H',
                  'Content-Type': 'application/json'
                }
              },
              wrapPayload: (msg) => {
                console.log(msg)

                return JSON.stringify({
                  'messages': [
                    {
                      'role': 'system',
                      'content': '你是聪明的助手'
                    },
                    {
                      'role': 'user',
                      'content': '老鼠生病了可以吃老鼠药治好吗？'
                    }
                  ],
                  'model': 'Qwen2.5-72B-Instruct',
                  'stream': false,
                  'max_tokens': 512,
                  'temperature': 0.7,
                  'top_p': 0.7,
                  'frequency_penalty': 1
                })
              },
              parseMessage: (message) => {
                console.log(message)

                return {
                  role: 'assistant', // user or assistant
                  content: message
                  // index: number,
                  // //0 代表首个文本结果；1 代表中间文本结果；2 代表最后一个文本结果。
                  // status: 0|1|2,
                }
              }
            }
          }
        },
        // 工具栏
        toolbarSize: 'medium'
      })
      return () => {
        aiEditor.destroy()
      }
    }
  }, [])

  return (
    <>
      <div style={{ padding: '20px' }}>
        <div ref={divRef} style={{ height: '600px' }} />
        <Button style={{ marginTop: '20px', float: 'right' }}>提交</Button>
      </div>
    </>
  )
}

export default Test